package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000122607982";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCAX3IeT4PUIvU4kCcqtnsn9DTR0KY0xDwZM6KKcmIz+aQWjaNaBNt4rmczqBSecx9NlYprXNeuXv/W3oQKDiibno6vkXkwh9Ua3s48cNcNL8Vt6IS0iiRjErDslK2xJLJVGP9Hys2Y3HjYmtem/MNp5y0+aDkp56X7WELM7sIgEdo1ayLSvLEzWW2QXclGRjHGl4CQAElzbxJhA9KJE2u0QkWw9AWpGLut6SJfN5cnJSmtr5GZWdPbjpRU/9GSmXGM2dfrzNzW9PWhlg6ayPq2wM4velIVDUm/eaVfGQ06UH3OdyCFEscn9Hd9VqacJ2LcyElzTgKTNPy2Bk76NBIRAgMBAAECggEAF47sh2l6POUJcY7q0SjZ4xQq2VJcHhThHpLeQmAJa5ZlqYnmr29yw2vmHpG67vUbioZl95v7LNHcS5dcTegZRa3Ma6Xbwq86yYqX+HmQzae1qJziNaugT9ovQ8tpMLgWTg2fU5KBNotHViDccJW0WuwE3o4L54fOaxjgcXFjiLTbNVOxyTUTPQ9BmWKAP8LBPFVgLWvDvzDnv3m8cBxPAkpU4TAZVviyYpTxMFfogDDt588HuAqQADf8I6nIIX76QrCmQRZljFuZE4lxBBaZ0XvrI6M9GIgc++MUcxNngYS2bUJTMsLMWVbjzVE8gNaslxq/oCUsfQfxe4nwyYVijQKBgQDGEpGaH6tzToG/zYbImeMI434JmeWfycFrp7wd/hrUF+Tm/Mx93R8k3SKigmtSAcZt8+t3z1IWFpWP/FBjoDmHM38z/roAaOyModEqFWxnbZGokEBtc2SQsVUuT0Bc5OdZ39M+/eg3uX5s4/ZV1yvHnoPqympLYXpKifoe2p+A1wKBgQCl6o1EDlefjHRul/KTN9QD5CEjSugVwE/pVs2oB8BwThy48WShNci7QBHrLXe6RxO4qobqR09zd3OSKFOQ3sotcqShNQahMpnNQCr3QxuzBUoJ2pq/XZVy1aZHH6+Yc/jReRYA9vqPLFJ4bXarRgheWazt+hOStOb65OcUnNzfVwKBgBkUPM9Q9zpwVEle0pQPuAlmFI1OqR5kQ1NIN69sfyUfqWH93D6Lguf9/+ansML/aop9efD/lRsq3OEQR3V10hLmWFLTC+YFYv4jdwJnGAS4gNrg+Odi1vbjHS/x5OpeKVTd3Dd7mq2DlRhgfM49olNyJK3T10WYIA3UDpFdiRidAoGBAJjlXy06MlFbc+h+qhI0vnKfhlIps1poELwtO/P+wL49nYZpHZzaHZNBZcHd8pl86pWgwBehg3watiwYcXntdU8QeNn+LY/bO5TD71RRSXNeCm6uPzDeotxiOosMjhrltIQFG9vjvd62r4E/Dr6jGQ8pgI0FiYgGoVzQGIsMSxadAoGBAI3Xtbmqml1t/iuGCj7FXcnL75n0vdGuykT32iWm/5iMk/0dwPcxQYDSueRHTroPaMPpTicHl5EoimQSfu+DLzWf03uwjslTtdgLAXpFjSRWmK/9acLCHlQ+wHGKQlNVXqal/cqSgwvIRBnzUdK4oLGnqXHKHttikIBufvAB1qyU";    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。

    public  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjR1YLKS7hJpX+GrJZu9+f6HAZLzAKMNyZm1NK0/Mo4930dOQE1fbQr1x2bM+cSDIVWvtLtOQ2MOcFq1p8FjxhH7iYQLrC2iddzcNxE5+aNlWJy4GumAKnks5QrWhQ+/Qky6bQSlL8ADLxrqOXnc8E16+JSa2dL2qZ7rC4g5qJTuiCXm5Aky0s1casfFt9LEPiF+dLzksOhzxPwUj7OIjMT3pltZljCGkdUm7cjc4od01tQSlE+vSWdwR8lgibvKE1IFAXyKH4To7l6bZKyNYXFcWVxeQ1m94wFVX6ElP6P4Z+vxng9Eb+5v/T+coPSkz4+//cWoKNFL7fJhWIZjnvQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息

    //支付成功之后异步回调要做的事
    private String notify_url="http://   /payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页

    //支付成功返回要返回的某个页面
    private  String return_url= "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    private  String timeout="30m";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","   //收单timeout,这里先写死
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝 的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
